<template>
	<div class="system-role-container layout-padding">
		<div class="system-role-padding layout-padding-auto">
			<el-card shadow="hover" class="pageQueryBoxArea mb10">
				<el-form :model="state.param" :inline="true">
					<el-form-item label="规格搜索:">
						<el-input v-model="state.tableData.param.keyWords" placeholder="请输入标题名称" clearable></el-input>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" @click="getTableData()">
							<el-icon>
								<ele-Search />
							</el-icon> 搜 索
						</el-button>
						<el-button type="primary" @click="onOpenAdd('add',{})">
							<el-icon>
								<ele-Plus />
							</el-icon> 新 增
						</el-button>
					</el-form-item>
				</el-form>
			</el-card>
			<el-card shadow="hover" style="display: flex;flex-direction: column;flex: 1;height: inherit;">
				<section style="display: flex;flex-direction: column; flex: 1;height: inherit;">
					<el-table :data="state.tableData.data" v-loading="state.tableData.loading">
						<el-table-column prop="tplId" label="ID" show-overflow-tooltip width="90"></el-table-column>
						<el-table-column prop="typeName" label="模板类型" show-overflow-tooltip></el-table-column>
						<el-table-column prop="tplName" label="模板名称" show-overflow-tooltip></el-table-column>
						<el-table-column label="商品规格" show-overflow-tooltip>
							<template #default="scope">
								<span v-for="(item, index) in scope.row.specList" :key="index">{{ item.specName }}、</span>
							</template>
						</el-table-column>
						<el-table-column label="商品属性" show-overflow-tooltip>
							<template #default="scope">
								<p v-for="(item, index) in scope.row.specList" :key="index">
									<span v-for="(item1, index1) in item.specItemList" :key="index1">{{ item1.specItemName }}、</span>
								</p>
							</template>
						</el-table-column>
						<el-table-column prop="sourceName" label="模板来源" show-overflow-tooltip></el-table-column>
						<el-table-column label="创建人" show-overflow-tooltip>
							<template #default="scope">
								<span>{{ scope.row.createdBy }}</span>
							</template>
						</el-table-column>
						<el-table-column label="创建时间" show-overflow-tooltip>
							<template #default="scope">
								<span>{{ scope.row.createdTime }}</span>
							</template>
						</el-table-column>
						<el-table-column label="操作" width="100">
							<template #default="scope">
								<el-button  text type="primary"
									@click="onOpenAdd('edit', scope.row)">编辑</el-button>
								<el-button  text type="primary" @click="onRowDel(scope.row)">删除</el-button>
							</template>
						</el-table-column>
					</el-table>
					<el-row justify="end">
						<el-col :span="24" class="flex-end-center">
							<el-pagination @size-change="onHandleSizeChange" @current-change="onHandleCurrentChange" class="mt15"
								:pager-count="5" :page-sizes="[10, 20, 30]" v-model:current-page="state.tableData.param.pageNum"
								background v-model:page-size="state.tableData.param.pageSize"
								layout="total, sizes, prev, pager, next, jumper" :total="state.tableData.total">
							</el-pagination>
						</el-col>
					</el-row>
				</section>
			</el-card>
		</div>
		<addSpecsDialog ref="addSpecsDialogRef" @refresh="getTableData()" />
	</div>
</template>

<script setup lang="ts" name="systemRole">
import { defineAsyncComponent, reactive, onMounted, ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import { templateList, templateDelete } from '/@/api/commodity';

// 引入组件
const addSpecsDialog = defineAsyncComponent(() => import('/@/views/commodity/goodspecsTemp/components/addSpecsDialog.vue'));

// 定义变量内容
const addSpecsDialogRef = ref();
const state = reactive<SysRoleState>({
	tableData: {
		data: [],
		total: 0,
		loading: false,
		param: {
			keyWords: '',
			pageNum: 1,
			pageSize: 10,
		},
	},
});


// 获取模板列表
const getTableData = async () => {
	state.tableData.loading = true;
	let params = {
		// keyWords: state.tableData.param.keyWords,
		pageNum: state.tableData.param.pageNum,
		pageSize: state.tableData.param.pageSize,
	}
	const { results } = await templateList(params);
	state.tableData.loading = false;
	state.tableData.data = results.records
	state.tableData.total = results.total;
}

// 打开修改角色弹窗
const onOpenAdd = (type: string, row: Object) => {
	console.log(JSON.stringify(addSpecsDialogRef))
	addSpecsDialogRef.value.openDialog(type, JSON.parse(JSON.stringify(row)));
};
// 删除模板
const onRowDel = (row: any) => {
	ElMessageBox.confirm(`此操作将删除该规格模板，是否继续?`, '提示', {
		confirmButtonText: '确认',
		cancelButtonText: '取消',
		type: 'warning',
	}).then(async () => {
		state.tableData.loading = true;
		const { results } = await templateDelete(row.tplId);
		ElMessage.success('删除成功');
		state.tableData.loading = false;
		getTableData();
	}).catch(() => { });
};
// 分页改变
const onHandleSizeChange = (val: number) => {
	state.tableData.param.pageSize = val;
	getTableData();
};

// 分页改变
const onHandleCurrentChange = (val: number) => {
	state.tableData.param.pageNum = val;
	getTableData();
};

// 页面加载时
onMounted(() => {
	getTableData();
});
</script>

<style scoped lang="scss">
@import '../../../theme/mixins/index.scss';
:deep(.el-card__body){
	display: flex;
	flex-direction: column;
	flex: 1;
	height: inherit;
	.el-table{
		flex: 1;
		.el-table__inner-wrapper::before{
			background: none !important;
		}
	}
}
</style>

